package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;

public class IOUtil 
{
	public static void gravar( Object obj, String strPath ) throws FileNotFoundException, IOException
	{
		ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream( new File( strPath ) ) );
		
		oos.writeObject( obj );
		
		oos.flush( );
		oos.close( );
	}
	
	public static String toString( InputStream is )
	{
		String strRet = "";
		
		Scanner sc = new Scanner( is );
		
		while( sc.hasNextLine( ) )
			strRet = strRet.concat( sc.nextLine( ) );
		
		sc.close( );
		
		return strRet;
	}

	public static Object ler( String strPath ) throws FileNotFoundException, IOException, ClassNotFoundException
	{
		return ler( new File( strPath ) );
	}

	public static Object ler( File file ) throws FileNotFoundException, IOException, ClassNotFoundException
	{
		ObjectInputStream ois = new ObjectInputStream( new FileInputStream( file ) );
		
		Object readObject = ois.readObject( );
		
		ois.close( );
		
		return readObject;
	}
}